package cn.dt70.controller;

import cn.dt70.pojo.AqbBankcard;
import cn.dt70.pojo.AqbUser;
import cn.dt70.service.AqbBankcardService;
import cn.dt70.service.AqbUserService;
import cn.dt70.utils.Constants;
import cn.itcast.vcode.utils.VerifyCode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.Writer;

@Controller
@RequestMapping("/user/")
public class UserController {

    @Resource
    AqbUserService aqbUserService;

    @Resource
    AqbBankcardService aqbBankcardService;

    //跳转用户页面
    @RequestMapping("center")
    public String userCenter() {
        return "usercenter";
    }

    //注册页面
    @RequestMapping("register")
    public String register() {

        return "register";
    }

    @RequestMapping("doRegist")
    public String doRegist(AqbUser aqbUser,HttpSession session,Model model) {
        if (aqbUserService.addUser(aqbUser)==null) {
            return "register";
        } else {
            model.addAttribute("aqbUser", aqbUser);
            return "login";
        }
    }

    //正常访问login页面
    @RequestMapping("login")
    public String login() {
        return "login";
    }

    //表单提交过来的路径
    @RequestMapping("checkLogin")
    public String checkLogin(@RequestParam("userCode") String userCode,
                             @RequestParam("password") String password,
                             AqbUser aqbUser, Model model, HttpSession session) {
        aqbUser = aqbUserService.doLogin(userCode, password);
        String s = "";
        //跳转到成功页面
        if (aqbUser != null && userCode.equals(aqbUser.getUserCode()) && password.equals(aqbUser.getPassword())) {
            session.setAttribute(Constants.USER_SESSION, aqbUser);
            if(aqbUser.getType() == 1){
                s = "backend/main";
            }else if(aqbUser.getType() == 0){

                s = "usercenter";
            }
        } else {
            model.addAttribute("retCode", "F");

            model.addAttribute("retMsg", "用户名或密码失败");
            s = "login";
        }
        return s ;
    }

    /**
     * userCode防重校验
     * ajax请求
     * 返回json字符串
     */
    @RequestMapping("userCodeExists")
    @ResponseBody
    public String userCodeExists(@RequestParam("userCode") String userCode) {
        AqbUser aqbUser = aqbUserService.getByUserCode(userCode);
        if (aqbUser.getUserCode() == null) {
            return "{\"retCode\":\"T\"}";
        }
        return "{\"retCode\":\"F\"}";
    }

    /*退出*/
    @RequestMapping("loginout")
    public String loginout(HttpSession session) {
        session.removeAttribute(Constants.USER_SESSION);
        return "redirect:login";
    }

    @RequestMapping("card")
    public String card() {
        return "home-register-openbankid";
    }

    /*开通账户页面*/
    @RequestMapping("openCard")
    public String open(AqbUser aqbUser, AqbBankcard aqbBankcard) {
        if (aqbUser.getUserId() == null) {
            return "home-register-openbankid";
        }
        aqbBankcard=aqbBankcardService.addCard(aqbBankcard);
        if (aqbBankcard !=null) {
            return "usercenter";
        }
        return "card";
    }

    /*跳转忘记密码页面*/
    @RequestMapping("forget")
    public String forget() {
        return "forget";
    }

    /*设置页面*/
    @RequestMapping("shezhi")
    public String shezhi(){
        return "usercenter-accountcontrol-userverify";
    }

    /*更改*/
    @RequestMapping(value = "modify/{userId}",method =RequestMethod.GET)
    public String change(@PathVariable("userId")Integer userId,Model model) {
        model.addAttribute("aqbUser",aqbUserService.getByUserId(userId));
        return "usercenter-accountcontrol-userverify";
    }

    @RequestMapping(value = "modify",method = RequestMethod.GET)
    public String modify(AqbUser aqbUser,HttpSession session){
          AqbUser aqbUser1=(AqbUser) session.getAttribute(Constants.USER_SESSION);
        //调用service的修改方法
        if(aqbUserService.modifyAqbUser(aqbUser)){
            return "usercenter";
        }else{
            return "redirect:usercenter-accountcontrol-userverify/"+aqbUser.getUserId();
        }
    }

    @RequestMapping("verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("验证huoqu");
        VerifyCode vc = new VerifyCode();//1. 创建验证码类
        BufferedImage image = vc.getImage(); // 2. 得到验证码图片
        request.getSession().setAttribute("num", vc.getText());//3. 把图片上的文本保存到session中
        VerifyCode.output(image, response.getOutputStream());//4. 把图片响应给客户端
    }

    @RequestMapping("isVerifyCode")
    public void check(HttpServletRequest request, HttpServletResponse response, String verifycode) throws ServletException, IOException {
        System.out.println("验证方法");
        Writer out = response.getWriter();
        if (verifycode.equals(request.getSession().getAttribute("num"))) {
            request.getSession().setAttribute("result", true);
            System.out.println("正确");
            out.write("true");
        } else {
            request.getSession().setAttribute("result", false);
            System.out.println("失败");
            out.write("false");
        }
    }
}